package vip.xiaonuo.modular;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import vip.xiaonuo.modular.remoteapi.stairs.RemoteStairsConstant;
import vip.xiaonuo.modular.sync.BuildingAddEvent;
import vip.xiaonuo.modular.sync.DeviceEvent;
import vip.xiaonuo.modular.ybuildingmemberrl.entity.YBuildingMemberRl;
import vip.xiaonuo.modular.ybuildingmemberrl.mapper.YBuildingMemberRlMapper;
import vip.xiaonuo.modular.ydeviceevent.entity.YDeviceEvent;
import vip.xiaonuo.modular.ydeviceevent.mapper.YDeviceEventMapper;
import vip.xiaonuo.modular.yelevatordevice.entity.YElevatorDevice;
import vip.xiaonuo.modular.yelevatordevice.service.YElevatorDeviceService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BuildingEventListener {

    @Autowired
    private YElevatorDeviceService elevatorDeviceService;

    @Resource
    private YBuildingMemberRlMapper buildingMemberRlMapper;
    @Resource
    private YDeviceEventMapper deviceEventEntityMapper;
    /**
     * 设备关联楼栋新增
     */
    @EventListener(classes = BuildingAddEvent.class)
    public void onBuildingAddEvent(BuildingAddEvent buildingAddEvent) {
        // 查找楼栋下的设备与人员关联关系
        List<YBuildingMemberRl> yBuildingMemberRls = buildingMemberRlMapper.selectAllByDeviceId(buildingAddEvent.getDeviceId(), RemoteStairsConstant.DeviceSyncStatus.UN_SYNC);
        if (CollectionUtils.isNotEmpty(yBuildingMemberRls)) {
            List<List<YBuildingMemberRl>> spilt = ListUtils.partition(yBuildingMemberRls, 6);
            YElevatorDevice elevatorDevice = elevatorDeviceService.getById(buildingAddEvent.getDeviceId());

            // 保存事件
            for (List<YBuildingMemberRl> buildingMemberRls : spilt) {
                YDeviceEvent deviceEvent = new YDeviceEvent();
                String memberRlIds = buildingMemberRls.stream()
                        .map(YBuildingMemberRl::getId)
                        .map(Objects::toString)
                        .collect(Collectors.joining(","));

                deviceEvent.setDeviceNo(elevatorDevice.getDeviceNo());
                deviceEvent.setBody(memberRlIds);
                deviceEvent.setType(RemoteStairsConstant.RequestCmd.BATCH_ADD_PERSON);
                deviceEvent.setStatus(DeviceEvent.NEW);
                deviceEvent.setCreateDate(new Date());
                deviceEvent.setSysType(1);
                deviceEventEntityMapper.insert(deviceEvent);
            }
            // 设置为新增状态
            elevatorDevice.setMemberSyncStatus(RemoteStairsConstant.MemberSyncStatus.ADD);
            elevatorDeviceService.updateById(elevatorDevice);
        } else {
            YElevatorDevice elevatorDevice = elevatorDeviceService.getById(buildingAddEvent.getDeviceId());
            elevatorDevice.setMemberSyncStatus(RemoteStairsConstant.MemberSyncStatus.SUCCESS);
            elevatorDeviceService.updateById(elevatorDevice);
        }
    }
}
